package ar.Location.Api.Wikipedia;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParserFactory;

import android.util.Log;
import android.util.Xml;
import ar.Location.Constant.Define;
import ar.Location.Helper.HttpProvider;
import ar.Location.PlaceApi.AbsAPI;
import ar.Location.PlaceObject.Place;
import ar.globals.Globals;

public class WikipediaAPI extends AbsAPI {

	private static final String TAG = "WikipediaAPI";

	@Override
	public ArrayList<Place> getListPlacesByGeoPoint(String latitude,
			String longitude, int radiusMetters) {
		// TODO Auto-generated method stub
		try {
			String url = Define.WIKIPEDIA_SEARCH_REQUEST;
			url = url.replace("@username", Define.WIKIPEDIA_USER_NAME);
			url = url.replace("@lat", latitude);
			url = url.replace("@lng", longitude);
			int radius = (int) (radiusMetters / Define.KM);
			if (radius < 1) {
				radius = 1;
			}
			url = url.replace("@radius", String.valueOf(radius));
			url = url.replace("@maxRows", String.valueOf(Globals.MAX_NUMBER_OF_SEARCHED_LOCATIONS));

			SAXParserFactory spf = SAXParserFactory.newInstance();
			spf.setValidating(false);
			InputStream inputStream = HttpProvider.getInputStream(url);
			WikipediaHandler wikipediaHandler = new WikipediaHandler();
			Xml.parse(inputStream, Xml.Encoding.UTF_8, wikipediaHandler);
			return wikipediaHandler.getListResults();
		} catch (Exception e) {
			// TODO: handle exception
			String msg = e.getMessage();
			if (msg != null) {
				Log.d(TAG, msg);
			}
			return null;
		}
	}

	@Override
	public ArrayList<Place> getListPlacesByType(String latitude,
			String longitude, String type, int radiusMetters) {
		// TODO Auto-generated method stub
		return null;
	}
}
